﻿using System;
using System.Xml.Serialization;
using System.Diagnostics;

namespace Data.Realm
{
    /// <summary>
    /// Stub of a class representing world. 
    /// <!--It'size a temporary version that should be checked and (probably) fixed-->
    /// </summary>
    /// <author>Pawel Gitlarski</author>
    [Serializable]
    [XmlType("w")]
    public class World
    {
        public int size;
        [XmlArrayItem("a")]
        [XmlArray("ar")]
        public Field[][] fields;
        public WorldParameters parameters;

        public Field GetField(int x, int y)
        {
            try 
            { 
                return this.fields[x][y]; 
            }
            catch(IndexOutOfRangeException) 
            { 
                return beyondWorld; 
            }
            // wiem ze brzydko jest doprowadzac do sytuacji wyjatkowych,
            // ae ladniej to wyglada niz stadko if'ow
        }
        
        /// <summary>
        /// Constructor for xml serialization
        /// </summary>
        public World() { }

        /// <summary>
        /// Create brand new world.
        /// </summary>
        /// <param name="stat">status field</param>
        /// <param name="source">number of source randomly placed on the world</param>
        /// <param name="obstacle">number of obstacle randomly placed on the world</param>
        /// <param name="noInfSource">if false, placed in corner infinite source</param>
        /// <param name="flatWorld">if false, generate fields with varying height</param>
        public World(int size, int source)
        {
            this.size = size;
            fields = new Field[size][];

            Debug.WriteLine(size);
            for (int i = 0; i < size; i++)
            {
                fields[i] = new Field[size];
                for (int j = 0; j < size; j++)
                {
                    fields[i][j] = new Field();
                }
            }
        }

        /// <summary>
        /// This field represent world outside our.
        /// </summary>
        static Field beyondWorld;

        static World() {
            beyondWorld = new Field();
            //beyondWorld.height = 100;
            //beyondWorld.agentId = -1;
            //beyondWorld.energy = 0;
            //beyondWorld.obstacle = true;
        }
    }
}
